package edu.szcu.vitaguard.dao;

import edu.szcu.vitaguard.entity.Schedule;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 医生排班DAO接口
 */
@Mapper
public interface ScheduleDao {

    /**
     * 插入排班
     * @param schedule 排班对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO schedule(doctor_id, dept_id, schedule_date, start_time, end_time, " +
            "max_appointments, status, create_time, update_time) " +
            "VALUES(#{doctorId}, #{deptId}, #{scheduleDate}, #{startTime}, #{endTime}, " +
            "#{maxAppointments}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "scheduleId")
    int insert(Schedule schedule);

    /**
     * 根据ID查询排班
     * @param scheduleId 排班ID
     * @return 排班对象
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.schedule_id = #{scheduleId}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    Schedule selectById(Integer scheduleId);

    /**
     * 查询所有排班
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectAll();

    /**
     * 根据医生ID查询排班
     * @param doctorId 医生ID
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.doctor_id = #{doctorId}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectByDoctorId(Integer doctorId);

    /**
     * 根据科室ID查询排班
     * @param deptId 科室ID
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.dept_id = #{deptId}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectByDeptId(Integer deptId);

    /**
     * 根据日期查询排班
     * @param scheduleDate 排班日期
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.schedule_date = #{scheduleDate}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectByDate(LocalDate scheduleDate);

    /**
     * 根据日期范围查询排班
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.schedule_date BETWEEN #{startDate} AND #{endDate}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectByDateRange(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 根据状态查询排班
     * @param status 状态
     * @return 排班列表
     */
    @Select("SELECT s.*, d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM schedule s " +
            "LEFT JOIN doctor d ON s.doctor_id = d.doctor_id " +
            "LEFT JOIN department dept ON s.dept_id = dept.dept_id " +
            "WHERE s.status = #{status}")
    @Results({
        @Result(property = "scheduleId", column = "schedule_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "scheduleDate", column = "schedule_date"),
        @Result(property = "startTime", column = "start_time"),
        @Result(property = "endTime", column = "end_time"),
        @Result(property = "maxAppointments", column = "max_appointments"),
        @Result(property = "status", column = "status"),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Schedule> selectByStatus(String status);

    /**
     * 更新排班
     * @param schedule 排班对象
     * @return 影响的行数
     */
    @Update("UPDATE schedule SET doctor_id = #{doctorId}, dept_id = #{deptId}, " +
            "schedule_date = #{scheduleDate}, start_time = #{startTime}, end_time = #{endTime}, " +
            "max_appointments = #{maxAppointments}, status = #{status}, update_time = #{updateTime} " +
            "WHERE schedule_id = #{scheduleId}")
    int update(Schedule schedule);

    /**
     * 删除排班
     * @param scheduleId 排班ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM schedule WHERE schedule_id = #{scheduleId}")
    int deleteById(Integer scheduleId);
}
